package net.aiform.record.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.aiform.bean.Constant;
import net.aiform.record.pojo.Record;
import net.aiform.record.pojo.Value;
import net.aiform.record.service.RecordService;
import net.aiform.template.pojo.Field;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

import ext.cjt.CommonMethods;

public final class SaveRecordAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private TemplateService templateService;
	private RecordService recordService;
	private HttpServletRequest req;
	private Template template;
	private String code;
	private String arg1;

	public String execute() {
		save();
		return Action.SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private void save() {
		List<Map<String, Object>> params = (List<Map<String, Object>>) req.getAttribute(Constant.PARAMS);
		Template t = this.templateService.get(this.code);
		for (Iterator<Map<String, Object>> iterator = params.iterator(); iterator.hasNext();) {
			Map<String, Object> p = (Map<String, Object>) iterator.next();
			this.recordService.save(getRecord(t, p));
		}
	}

	private Record getRecord(Template t, Map<String, Object> params) {
		Record r = new Record();
		r.setCode(CommonMethods.getUUID());
		r.setTemplate(t);
		r.setValues(new ArrayList<Value>());
		Set<Field> fs = t.getFields();
		for (Iterator<Field> iterator = fs.iterator(); iterator.hasNext();) {
			Field f = (Field) iterator.next();
			String code = f.getCode();
			String value = (String) params.get(code);
			Value v = Value.getInstance(t.getTable());
			v.setCode(code);
			v.setRecord(r);
			v.setField(f);
			v.setTemplate(t);
			CommonMethods.initValue(f, v, value);
			r.getValues().add(v);
		}
		return r;
	}

	public SaveRecordAction() {
		super();
	}

	public SaveRecordAction(TemplateService templateService, RecordService recordService) {
		super();
		this.templateService = templateService;
		this.recordService = recordService;
	}

	public void prepare() throws Exception {

	}

	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public RecordService getRecordService() {
		return recordService;
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}
}
